using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using EventUtils;
using UnityEngine;
using UnityEngine.UI;
using EventType = EventUtils.EventType;

namespace View.Instant
{
    public class SeeAdvView : View
    {
        private Button mbtnClose;
        private Button mbtnGo;
        private Image mimgIcon;
        private Text mtxtName;
        private PropItemEnum mPropItem = PropItemEnum.none;
        public override void OnLoad()
        {
            base.OnLoad();
            mbtnClose = transform.Find("btnClose").GetComponent<Button>();
            mbtnGo = transform.Find("btnGo").GetComponent<Button>();
            mimgIcon = transform.Find("icon/icon").GetComponent<Image>();
            mtxtName = transform.Find("icon/name").GetComponent<Text>();
            
            AddClickListener(mbtnClose,() =>
            {
                GameHelp.GamePause = false;
                GameHelp.CanRayCast = true;
                UIManager.Instance.Close(ViewName.SeeAdvView);
                if (GameHelp.GameType == 2)
                {
                    UIManager.Instance.Show(ViewName.GameEndView,ViewLayer.Game);
                }
            });
            AddClickListener(mbtnGo,() =>
            {
                UIManager.Instance.OnShowLoading();
                AndroidManager.Ins.LoadReward(() =>
                {
                    UIManager.Instance.OnCloseLoading();
                    TaskManager.OnAddTaskNumByTaskType(TaskTypeEnum.adv);
                    EventCenter.BroadCast<string>(EventType.EVENT_SEE_ADV_END,ViewName.SeeAdvView);
                });
            });
        }
        private void OnSeeAdvCallback(string str)
        {
            if (str == ViewName.SeeAdvView && mPropItem != PropItemEnum.none)
            {
                Debug.LogError("次数加一");
                TaskManager.OnAddTaskNumByTaskType(TaskTypeEnum.adv);

                PropItemTable proptable = null;
                if (mPropItem == PropItemEnum.jiashi)
                {
                    proptable = GameLocalInfo.OnGetPropItemTableById(PropItemEnum.jiashi);
                    if (proptable != null)
                    {
                        GameHelp.GameTimer = proptable.useadd;
                        UIManager.Instance.Show(ViewName.ToolTipsView, ViewLayer.Top, string.Format("使用加时卡，增加{0}s时间！",proptable.useadd));
                    }
                }
                else
                {
                    proptable = GameLocalInfo.OnGetPropItemTableCloneById(mPropItem);
                    GameHelp.OnAddSelfPlayerPropItem(proptable);
                    EventCenter.BroadCast<List<PropItemEnum>>(EventType.EVENT_GONGXIHUODE,new List<PropItemEnum>(){proptable.enumid});
                }

                EventCenter.BroadCast<PropItemEnum>(EventType.EVENT_ADV_END_TO_GAME,mPropItem);
                UIManager.Instance.Close(ViewName.SeeAdvView);
                GameHelp.CanRayCast = true;
            }
        }
        public override void OnShow(params object[] args)
        {
            base.OnShow();
            mPropItem = (PropItemEnum)args[0];
            var itemdata = GameLocalInfo.OnGetPropItemTableCloneById(mPropItem);
            mimgIcon.sprite = ResourceManager.Instance.LoadSprite(itemdata.path, itemdata.iconname);
            mimgIcon.SetNativeSize();
            mtxtName.text = itemdata.itemname;
            
            EventCenter.AddListener<string>(EventType.EVENT_SEE_ADV_END,OnSeeAdvCallback);
        }

        public override void OnClose(params object[] args)
        {
            base.OnClose();
            if (gameObject.activeSelf)
            {
                EventCenter.RemoveListener<string>(EventType.EVENT_SEE_ADV_END,OnSeeAdvCallback);
            }
        }
    }
    
}
